﻿using System;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;

namespace Xenta.Web.Controllers
{
    public sealed class StaticController : ExtController
    {
        [HttpGet]
        public ActionResult Home()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Privacy()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Terms()
        {
            return View();
        }

        [HttpGet, RestoreState]
        public ActionResult Contact()
        {
            return View();
        }

        [HttpPost, PersistState]
        public ActionResult Contact(string email,
            string subject,
            string text)
        {
            email = StringHelper.EnsureNotNull(email).Trim();
            subject = StringHelper.EnsureNotNull(subject).Trim();
            subject = HtmlUtility.Encode(subject);
            text = StringHelper.EnsureNotNull(text).Trim();
            text = HtmlUtility.Encode(text);

            try
            {
                Svc.ProcessOperation("EnqueueContactMessage", new
                {
                    LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                    ReplyTo = email,
                    Subject = subject,
                    Text = text
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Contact").Localized());
        }
    }
}
